﻿// Admin.NET 项目的版权、商标、专利和其他相关权利均受相应法律法规的保护。使用本项目应遵守相关法律法规和许可证的要求。
//
// 本项目主要遵循 MIT 许可证和 Apache 许可证（版本 2.0）进行分发和使用。许可证位于源代码树根目录中的 LICENSE-MIT 和 LICENSE-APACHE 文件。
//
// 不得利用本项目从事危害国家安全、扰乱社会秩序、侵犯他人合法权益等法律法规禁止的活动！任何基于本项目二次开发而产生的一切法律纠纷和责任，我们不承担任何责任！

using Admin.NET.Plugin.MQTT;
using Admin.NET.Plugin.MQTT.Option;

using Furion;

using Microsoft.CodeAnalysis.CSharp.Syntax;

using MQTTnet.Protocol;
using MQTTnet.Server;

using System.Buffers;

namespace Admin.NET.Plugin.MQTT.MQTT;
public class MyMqttService
{
    public Task Server_ApplicationMessageNotConsumedAsync(ApplicationMessageNotConsumedEventArgs arg)
    {
        Console.WriteLine($"客户端：【{arg.SenderId}】,主题【{arg.ApplicationMessage.Topic}】" +
            $"收到消息：{System.Text.Encoding.Default.GetString(arg.ApplicationMessage.Payload.ToArray())}");
       var server= App.GetService<MqttServer>();
     
        return Task.CompletedTask;
    }
    /// <summary>
    /// 客户端验证账号密码
    /// </summary>
    /// <param name="arg"></param>
    /// <returns></returns>
    public Task Server_ValidatingConnectionAsync(ValidatingConnectionEventArgs arg)
    {
        var myoption = App.GetConfig<MyMqttServerOptions>("MyMqttServer", true);
        if(arg.UserName!=myoption.UserName || arg.Password!=myoption.UserPassWord) arg.ReasonCode = MqttConnectReasonCode.BadUserNameOrPassword;
        else arg.ReasonCode = MqttConnectReasonCode.Success;
        return Task.CompletedTask;
    }
    /// <summary>
    /// 侦听所有消息
    /// </summary>
    /// <param name="arg"></param>
    /// <returns></returns>
    public async Task Server_InterceptingPublishAsync(InterceptingPublishEventArgs arg)
    {
        //Console.WriteLine($"侦听所有消息：【{arg.ClientId}】,主题【{arg.ApplicationMessage.Topic}】收到消息：{System.Text.Encoding.Default.GetString(arg.ApplicationMessage.Payload.ToArray())}");
        await Task.CompletedTask;
    }
    /// <summary>
    /// 客户端断开连接
    /// </summary>
    /// <param name="arg"></param>
    /// <returns></returns>
    public Task Server_ClientDisconnectedAsync(ClientDisconnectedEventArgs arg)
    {
        Console.WriteLine($"【{arg.ClientId}】 下线了，{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
        return Task.CompletedTask;
    }

    /// <summary>
    /// 客户端上线
    /// </summary>
    /// <param name="arg"></param>
    /// <returns></returns>
    public Task Server_ClientConnectedAsync(ClientConnectedEventArgs arg)
    {
        Console.WriteLine($"{arg.ClientId} 上线了，{DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")}");
        return Task.CompletedTask;
    }
    /// <summary>
    /// 客户端取消订阅
    /// </summary>
    /// <param name="arg"></param>
    /// <returns></returns>
    public Task Server_ClientUnsubscribedTopicAsync(ClientUnsubscribedTopicEventArgs arg)
    {
        Console.WriteLine($"客户端取消订阅了主体{arg.ClientId}-{arg.TopicFilter}");
        return Task.CompletedTask;
    }
    /// <summary>
    /// 客户端订阅
    /// </summary>
    /// <param name="arg"></param>
    /// <returns></returns>
    public Task Server_ClientSubscribedTopicAsync(ClientSubscribedTopicEventArgs arg)
    {
        Console.WriteLine($"客户端订阅了主体{arg.ClientId}-{arg.TopicFilter.Topic}");
        
        return Task.CompletedTask;
    }
    /// <summary>
    /// 客户端保留消息
    /// </summary>
    /// <param name="arg"></param>
    /// <returns></returns>
    public Task Server_RetainedMessageChangedAsync(RetainedMessageChangedEventArgs arg)
    {
        Console.WriteLine($"收到消息：{arg.ClientId}");
        return Task.CompletedTask;
    }
}
